<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="360px">
    <el-form ref="formItem" :model="formItem" label-width="100px" size="small" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择客户" prop="custId">
            <el-select v-model="formItem.custId" clearable filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteCustMethod" @change="handleChange">
              <el-option v-for="item in custOption" :key="item.id" :label="item.name" :value="item.custId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="选择项目" prop="projectId">
            <el-select v-model="formItem.projectId" clearable filterable placeholder="先选客户,再选项目">
              <el-option v-for="item in projectOption" :key="item.id" :label="item.projectName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="收款日期" prop="payDate">
            <el-date-picker v-model="formItem.payDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="收款方式" prop="payType">
            <el-select v-model="formItem.payType" placeholder="请选择">
              <el-option label="银行承兑" :value="1" />
              <el-option label="银行转账" :value="2" />
              <el-option label="现金" :value="3" />
              <el-option label="支票" :value="3" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="收款金额" prop="count">
            <el-input v-model="formItem.count"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" :rows="2" v-model="formItem.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addPay, updatePay } from '@/api/account'
import { getProjectList2 } from '@/api/project'
import { getTenCust } from '@/api/cust'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      projectList: [],
      projectOption: [],
      custOption: [],
      formItem: {
        id: '',
        payId: '',
        custId: '',
        projectId: '',
        payType: '',
        payDate: '',
        count: null,
        remark: ''
      },
      rules: {
        carNo: [{ required: true, message: '请输入', trigger: 'blur' }],
        driver: [{ required: true, message: '请输入', trigger: 'blur' }],
        custId: [{ required: true, message: '请选择客户', trigger: 'blur' }],
      }
    }
  },
  methods: {
    init (row) {
      if (row) {
        this.title = '编辑'
        this.formItem = _.clone(row)
        this.isEdit = true
        this.custOption = [{ custId: row.custId, name: row.custName }]
        delete this.formItem.updateTime
      } else {
        this.title = '新增'
        this.formItem = {
          id: '',
          payId: '',
          custId: '',
          projectId: '',
          payType: 1,
          payDate: '',
          count: null,
          remark: ''
        }
        this.isEdit = false
        this.custOption = []
        this.projectOption = []
      }
      this.dialogVisible = true
      this.queryProject()
    },
    async queryProject () {
      const res = await getProjectList2()
      if (res.code === 200) {
        this.projectList = res.data
        if (this.formItem.custId) {
          this.handleChange(this.formItem.custId)
        } else {
          this.projectList = []
        }
      } else {
        this.projectList = []
        this.$message.error(res.msg)
      }
    },
    async remoteCustMethod (keyword) {
      if (keyword) {
        const res = await getTenCust({ keyword })
        if (res.code === 200) {
          this.custOption = res.data
        } else {
          this.custOption = []
        }
      } else {
        this.custOption = [];
        this.$message.error(res.msg)
      }
    },
    handleChange (custId) {
      if (custId) {
        const arr = this.projectList.filter(p => p.ifUse === 1)
        const arr1 = arr.filter(a1 => a1.custId === custId)
        const arr2 = arr.filter(a2 => !a2.custId)
        this.projectOption = arr1.length > 0 ? arr1 : arr2
      } else {
        this.projectOption = [];
      }
    },
    async handleSubmit () {
      const res = this.isEdit ? await updatePay(this.formItem) : await addPay(this.formItem)
      if (res.code === 200) {
        this.$emit('ok')
        this.dialogVisible = false
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<style>
</style>